package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.common.Result;
import com.example.demo.service.ClassRoomService;
import com.github.pagehelper.PageInfo;

import jakarta.annotation.Resource;
import com.example.demo.entity.ClassRoom;
import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/classroom")
public class ClassroomController {
    @Resource
    private ClassRoomService classRoomService;

    /**
     * 查询所有教室信息
     * @return Result对象，包含教室列表数据
     */
    @GetMapping("/selectAll")
    public Result selectAll(ClassRoom classRoom){
        List<ClassRoom> list = classRoomService.selectAll(classRoom);
        return Result.success(list);
    }

    /**
     * 分页查询教室信息
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return Result对象，包含分页教室数据
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(ClassRoom classRoom,
                                @RequestParam(defaultValue = "1") int pageNum, 
                                @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<ClassRoom> list = classRoomService.selectByPage(classRoom, pageNum, pageSize);
        return Result.success(list);
    }

    /**
     * 插入教室信息
     * @param classRoom 教室对象，包含教室名称、座位数量、描述信息
     * @return Result对象，包含操作结果
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody ClassRoom classRoom){
        int rows = classRoomService.insert(classRoom);
        if(rows > 0){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    /**
     * 更新教室信息
     * @param classRoom 教室对象，包含ID、教室名称、座位数量、描述信息
     * @return Result对象，包含操作结果
     */
    @PutMapping("/update")
    public Result update(@RequestBody ClassRoom classRoom){
        int rows = classRoomService.update(classRoom);
        if(rows > 0){
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    /**
     * 删除教室信息
     * @param id 教室ID
     * @return Result对象，包含操作结果
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<Integer> idList){
        int rows = classRoomService.delete(idList);
        if(rows > 0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    
}
